var benchmarkLib = [];
//var testCSV = jsCSVTester.genCSV(50000,["firstName","lastName","favoriteCar","favoriteColors","favoriteMonths"]);
var testCSV = jsCSVTester.genCSV(500,[200,"firstName","lastName","favoriteCar","favoriteColors","favoriteMonths"]);

(function (){
    var test = { 
        name : "dataset test",
        code : function (){
                var rCSVData = null;
                var onComplete = function(data){
                    rCSVData = data;
                }
 
                //rCSV.toArrays(testCSV,onComplete);
                //var jsCSVData = (jsCSV.parse(testCSV)).data;
                
                //deepEqual (rCSVData, jsCSVData, "csv parsing: the same data");
                equal(1,1,"dummy");
            }
    };
    benchmarkLib.push(test); //comment this line to remove from the test
})();   


(function (){

    var userAgentsSamples = [
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
        "Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
        "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; FDM)",
        "Mozilla/5.0 (Windows NT 5.2; rv:8.0.1) Gecko/20100101 Firefox/8.0.1",
        "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)",
        "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",
        "Mozilla/5.0 (Linux; Android 4.0.4; GT-P7510 Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19",
        "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; GT-P7510 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30"
    ];

    var browsers = {
        "chrome":function(ua){
                var r = /(Macintosh|Windows|Linux).+(Chrome.\d+)/g.exec(ua);
                return (null!=r)?r[1]+" "+r[2]:null;
            },
        "chrios":function(ua){
                var r = /(iPad|iPhone).+CriOS(.\d+)/g.exec(ua);
                return (null!=r)?r[1]+" Chrome"+r[2]:null;
            },
        "safari":function(ua){
                var r = /(Macintosh|iPad|iPhone|Windows).+Version(.\d+).+(Safari)/g.exec(ua);
                return (null!=r)?r[1]+" "+r[3]+""+r[2]:null;
            },
        "firefox":function(ua){
                var r = /(Macintosh|Windows|Linux).+(Firefox.\d+)/g.exec(ua);
                return (null!=r)?r[1]+" "+r[2]:null;
            },
        "ie":function(ua){
                var r = /(MSIE).(\d+)/g.exec(ua);
                return (null!=r)?"Windows "+r[1]+"/"+r[2]:null;
            }
    };

    function browser(ua){
       for (name in browsers){
           var v = browsers[name](ua);
           if(null != v){return v;} 
       }
       return "undetected undetected";
    }

    function benchmark(name, ref, test, iterations){
        var min = undefined; var max = undefined; 
        var count = iterations || 1000;
        var total = 0;
        for(var i = 0; i < count; ++i){
            var start = (new Date()).valueOf();
            test();
            var duration = (new Date()).valueOf() - start;
            min = ( !min || min > duration ) ? duration : min ;
            max = ( !max || max < duration ) ? duration : max ;
            total += duration;
            progress(name+":"+i+"/"+count);
        }
        return {name:name,min:min,max:max,average:(total/count),ref:ref};
    }

    function tagAttributes(attributes){
        return ((attributes || {})["class"])?" class='"+attributes["class"]+"' ":"";
    }

    function divTag(content,attributes){
        return "<div"+tagAttributes(attributes)+">"+content+"</div>";
    }

    function benchmarkTemplate(benchmark){
        return divTag(
            divTag(""+benchmark.name, {"class":"name"})+ 
            divTag("min:"+benchmark.min, {"class":"min value"})+ 
            divTag("max:"+benchmark.max, {"class":"max value"})+ 
            divTag("average:"+benchmark.average, {"class":"average value"}),
            {"class":"benchmark"}
        );
    }
    
    function progress(message){
        $("#progress").text(message);
    }

    function display(benchmark){
        $("#message").append(benchmarkTemplate(benchmark));
        $("input[type='text'][name='"+benchmark.ref+"']").val(benchmark.average);
    }

    runBenchmarks = function(){
        $("#message").text("sleeping 3 sec... and then running tests, it will take a few moments");
        setTimeout(function(){
                $("#message").text("");
                var iterations = 50;
                display(benchmark("jQuery-CSV","entry.4.single",function(){ $.csv.toArrays(testCSV); },iterations));
                display(benchmark("rCSV","entry.5.single",function(){ rCSV.toArrays(testCSV); },iterations));
                //display(benchmark("jsCSV","entry.6.single",function(){ jsCSV.parse(testCSV); },iterations));
                //display(benchmark("nCSV","entry.7.single",function(){ nCSV.parse(testCSV); },iterations));
                var ua = navigator.userAgent;
                $("input[type='text'][name='entry.0.single']").val(ua);
                var ub = browser(ua).split(" "); 
                $("input[type='text'][name='entry.2.single']").val(ub[1]);
                $("input[type='text'][name='entry.3.single']").val(ub[0]);
                progress("completed");
            },
        3000);
    }


})();
